using System;
using System.Web.Mvc;
using MvcCms.Data;
using MvcCms.Service.Cache;
using Microsoft.Practices.ServiceLocation;
namespace MvcCms.Service.Helpers
{
    public static class IsCurrentRouteHelper
    {
        public static bool IsCurrentAction(this HtmlHelper helper, string actionName, string controllerName)
        {
            var currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
            var currentActionName = (string)helper.ViewContext.RouteData.Values["action"];

            if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) &&
                currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase))
                return true;

            return false;
        }

        public static bool IsCurrentRoute(this HtmlHelper helper, int contentCatId, string path, bool isSubCategory)
        {
            var cacheService = ServiceLocator.Current.GetInstance<ICacheService>(); 
            
            var routeTokens = helper.ViewContext.RouteData.DataTokens;
            var currentPathName = (string)helper.ViewContext.RouteData.Values["path"];
            
            ContentCategory currentCategory;
            
            object oRouteCategoryId;
            if (helper.ViewContext.RouteData.DataTokens.TryGetValue("contentcatid", out oRouteCategoryId))
            {
                var routeCategoryId = int.Parse(oRouteCategoryId.ToString());
                if ((routeCategoryId == contentCatId) & isSubCategory) { return true; }
                currentCategory = cacheService.GetCorrectCategory(routeCategoryId, currentPathName);
                var currentCategoryId = currentCategory.CategoryID;

                string categoryPath;

                if (currentCategory.ParentList != null)
                {
                    categoryPath = currentCategory.ParentList;

                    if (categoryPath.Contains("," + contentCatId + ","))
                    {
                        return true;
                    }
                }

                if (currentCategoryId == contentCatId)
                {

                    var subCatToken = routeTokens["issubcatroute"];
                    if (subCatToken != null)
                    {
                        if (isSubCategory)
                        {
                            return true;
                        }
                        if (currentPathName != null)
                        {
                            if (currentPathName.Contains(path))
                            {
                                return true;
                            }
                        }
                        return false;
                    }

                    if (path != "")
                    {
                        return path == currentPathName;
                    }
                    return true;
                }
                return path == currentPathName;
            }
            
            object oArea;
            object oAreaPath;
            if (helper.ViewContext.RouteData.DataTokens.TryGetValue("area", out oArea))
            {
                helper.ViewContext.RouteData.DataTokens.TryGetValue("path", out oAreaPath);
                if (oAreaPath != null)
                {
                    currentCategory = cacheService.GetCorrectCategory(contentCatId, path);
                    var areaPath = oAreaPath.ToString();
                    if (currentCategory.UsesCustomAreaRoute)
                    {
                        if (currentCategory.ParentCategory == null)
                        {
                            return currentCategory.Path == areaPath;
                        }
                        if (currentPathName == null) { currentPathName = ""; }
                        if (currentCategory.Path.ToLower() == currentCategory.ParentCategory.Title.ToLower() + "/" + currentPathName.ToLower())
                        {
                            //this only works for children one level deep.  if you need deeper child
                            //cat nav links for a custom route area you'll need some code for it here
                            return currentCategory.ParentCategory.Path == areaPath;
                        }
                    }
                }
            }
            return false;
        }
    }
}